﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StorageInterface.Dialogs
{
    /// <summary>
    /// Interaction logic for MoveDialog.xaml
    /// </summary>
    public partial class MoveDialog : Window
    {
        bool Inventarize = false;
        public MoveDialog(Window win,bool isinv = false)
        {
            InitializeComponent();
            this.Owner = win;
            Inventarize = isinv;

            var list = Data.GetAllStorages();
            if (isinv)
            {
                Title = "Инвентаризация";
                fromcombo.Items.Add("Инв");
                tocombo.Items.Add("Инв");
            }
            foreach (Storage el in list)
            {
                fromcombo.Items.Add(el);
                tocombo.Items.Add(el);
            }
            comboBox1.ItemsSource = Data.GetAllProducts();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        double count1 = 0;
        double count2 = 0;
        double Count1 { get { return count1; } set { textBox1.Text = value.ToString(); count1 = value; } }
        double Count2 { get { return count2; } set { textBox2.Text = value.ToString(); count2 = value; } }
        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Count1 = Data.GetAvaibleAmount(fromcombo.SelectedItem as Storage, comboBox1.SelectedItem as Product);
            Count2 = Data.GetAvaibleAmount(tocombo.SelectedItem as Storage, comboBox1.SelectedItem as Product);
        }

        private void fromcombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Count1 = Data.GetAvaibleAmount(fromcombo.SelectedItem as Storage, comboBox1.SelectedItem as Product);
            Count2 = Data.GetAvaibleAmount(tocombo.SelectedItem as Storage, comboBox1.SelectedItem as Product);

        }

        private void tocombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Count1 = Data.GetAvaibleAmount(fromcombo.SelectedItem as Storage, comboBox1.SelectedItem as Product);
            Count2 = Data.GetAvaibleAmount(tocombo.SelectedItem as Storage, comboBox1.SelectedItem as Product);

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            double count = 0;
            if (comboBox1.SelectedItem as Product == null ||
                !double.TryParse(amountTB.Text, out count)||
                fromcombo.SelectedItem == null ||
                tocombo.SelectedItem == null )
            {
                ErrorHandler.HandleError("Не все поля заполнены верно!!!");
                return;
            }

            Storage st1 = fromcombo.SelectedItem as Storage;            
            Storage st2 = tocombo.SelectedItem as Storage;
            if (st1 != null && Count1 < count || st1 == st2)
            {
                ErrorHandler.HandleError("Не все поля заполнены верно!!!");
                return;
            }
            if (Inventarize)
            {
                if ( (st1 == null &&
                     st2 !=null) || (st1 != null && st2 == null) )
                {

                    Data.NewMove(st1, st2, comboBox1.SelectedItem as Product, count);
                    Close();
                    return;
                }
                else
                {
                    ErrorHandler.HandleError("Ошибка заполнения полей!!!");
                    return;
                }
            }
            else 
            {
                Data.NewMove(st1, st2, comboBox1.SelectedItem as Product, count);
                Close();
            }
        } 
  
    }
}
